#include "optlib.h"
#include "string.h"
#include "stdint.h"
#include "cpuid.h"
#include "text_video.h"
#include "gmp-glue.h"

void init_optimized_memory_functions()
{

    /*if(cpuid.features.SSE2 || cpuid.features.SSE4_1)
    {
        memclr = memclr_sse2;
        memcpy = memcpy_sse2;
        
        if(cpuid.features.SSE4_1){ memcmp = memcmp_sse4_1; }
        else{ memcmp = memcmp_std; }
    }
    else
    {*/
        memclr = memclr_std;
        memcpy = memcpy_std;
        memcmp = memcmp_std;
    //}
}

void stderr(int err)
{
    WARNING("STDERR: #%x\n", err);
}

void abort(int err)
{
    WARNING("ABORT: #%x\n", err);
    for(;;){asm("hlt;");}
}

uint8_t * malloc(const size_t count)
{
    size_t block;

    if(!count || count == (size_t)-1)
    {
        WARNING("SYS: malloc(%u)\n", count);
        return (uint8_t*)(0);
    }    
    
    block = base;
    base += count;
    
    memclr((void *)block, count);
    
    return (uint8_t*)block;
}

uint8_t * realloc(uint8_t * ptr, size_t count)
{
    return ptr;
}

void free(void * bleh)
{ }

int fprintf(uint8_t * out, const char * str, ...){ printf("%x", str); return strlen(str);}
int fputs ( const char * str, uint8_t * stream ){ printf("%x", str); return strlen(str);}

int __fprintf_chk(uint8_t * fp, int flag, const char * format, ...){ DEBUG("SYS: __fprintf_chk()\n"); return 0; }

int __snprintf_chk(uint8_t * fp, int flag, char *str, size_t size, const char *format, ...){ DEBUG("SYS: __snprintf_chk()\n"); return 0; }

int __memset_chk(uint8_t * fp, int flag, const void * const dest, const uint8_t val, const size_t count){ DEBUG("SYS: __memset_chk()\n"); return 1; }

size_t fwrite ( const void * ptr, size_t size, size_t count, uint8_t * stream )
{
    //printf("FWRITE{%u:%u}: ", count, size);
    printf("%s", ptr);
    return count;
}

char * strchr ( const char * str, int ch)
{
    size_t i;

    for(i = 0; i < strlen(str) + 1; i++)
    {
        if(str[i] == (char)ch){ return (char *)(str + i); }
    }
    
    return (char *)(0);
}

char *nl_langinfo(char ** item){ DEBUG("SYS: nl_langinfo()\n"); return (char *)(0); }
const unsigned short * * __ctype_b_loc  (void){  return (const unsigned short * *)(0); }
int raise (size_t sig){ WARNING("SIGNAL #%u\n",sig); return 0; }







